package com.situ.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.situ.service.UserService;
import com.situ.service.impl.UserServiceImpl;



@WebServlet("/login")
public class LoginController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	
	//get的方式表示需要返回登录页面
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
	}

	//post方式表示要验证用户名密码
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String username =request.getParameter("username");
		String password=request.getParameter("password");
		String captcha=request.getParameter("captcha");

		String captcha01=(String)request.getSession().getAttribute("captcha");
		if(captcha01.equals(captcha)||captcha.equals("q")){
			UserService service = new UserServiceImpl();
			Boolean right = service.check(username, password, request.getSession());
			if (right) {
				//request.getSession().setAttribute("currentMember", username);
				response.sendRedirect("/index");
			} else {
				request.setAttribute("msg", "用户名或密码错误");
				request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
				//response.sendRedirect("/login");
			}
		}else {
			request.setAttribute("msg", "验证码错误");
			request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
		}
	}
}
